package com.qf.controller;/*
 *徐韬
 *2021/10/29
 *20:17
 */

import com.github.pagehelper.PageInfo;
import com.qf.dto.R;
import com.qf.pojo.TAdminUser;
import com.qf.pojo.TSmsTemplate;
import com.qf.service.impl.smsTemplateServiceimpl;
import com.qf.service.smsTemplateService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpSession;
import java.lang.annotation.Target;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/sms")
@Api(tags = "短信模板管理")
public class smstemplateController {
    private com.qf.service.smsTemplateService smsTemplateService;
@Autowired
    public void setSmsTemplateService(com.qf.service.smsTemplateService smsTemplateService) {
        this.smsTemplateService = smsTemplateService;
    }

    @PostMapping("/add")
    public R add(@RequestBody TSmsTemplate smsTemplate, HttpSession session){
        TAdminUser user = (TAdminUser) session.getAttribute("user");
        if(user != null){
            Long type = user.getType();
            Long id = user.getId();
            smsTemplate.setCreater(id);
            smsTemplate.setOwntype(type);
        }
        smsTemplateService.addsms(smsTemplate);

        return R.setOk();

    }
    @GetMapping("/showAll")
    public R showAll(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo<TSmsTemplate> pageInfo = smsTemplateService.showAll(pageNum, pageSize);

        return R.setOk(pageInfo);

    }
    @PostMapping("/update")
    public R udpate(@RequestBody TSmsTemplate tSmsTemplate,HttpSession session){
        TAdminUser user = (TAdminUser) session.getAttribute("user");
        Long id = user.getId();
        Long type = user.getType();
        tSmsTemplate.setOwntype(type);
        tSmsTemplate.setCreater(id);
        smsTemplateService.update(tSmsTemplate);
        return R.setOk();
    }
    @DeleteMapping("/delete")
    public R delete(@RequestBody Map map){
        List<Integer> ids = (List<Integer>) map.get("ids");
        smsTemplateService.deleteByis(ids);
        return R.setOk();
    }
    @GetMapping("/showByparams")
    public R showAllByparams(TSmsTemplate tSmsTemplate){
        List<TSmsTemplate> templates = smsTemplateService.showAllByParams(tSmsTemplate);
        return R.setOk(templates);
    }
}
